package de.tum.in.eist.poll.client;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.RootPanel;

import de.tum.in.eist.poll.shared.Lecture;

public class LectureScreen implements ViewInterface {
	private boolean is_visible = false;
	
	// Widgets on this screen.
	private Anchor logout_link;
	private List<Anchor> lecture_links;
	
	public LectureScreen(final EIST_POLL controller) {
		lecture_links = new ArrayList<Anchor>();
		logout_link = new Anchor("Logout");
		logout_link.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(final ClickEvent event) {
				controller.showLoginScreen();
			}
		});
		
		logout_link.getElement().setId("backLink");
	}

	@Override
	public void show() {
		is_visible = true;
		
		// Change the subtitle of the page.
		DOM.setInnerHTML(DOM.getElementById("subtitle"), "Lectures");
		
		RootPanel r = RootPanel.get("content");
		r.add(logout_link);
		for(Anchor link : lecture_links) {
			r.add(link);
		}
	}

	@Override
	public void hide() {
		is_visible = false;
		
		RootPanel r = RootPanel.get("content");
		r.remove(logout_link);
		for(Anchor link : lecture_links) {
			r.remove(link);
		}
	}
	
	public boolean isVisible() {
		return is_visible;
	}

	/**
	 * Called to set the list of lectures that should be shown on this screen.
	 */
	public void SetLectures(List<Lecture> lectures, final EIST_POLL controller) {
		lecture_links.clear();
		
		for(final Lecture lecture : lectures) {
			String display_name = lecture.getLecturer().getName() + " - " + lecture.getLectureTitle() + " - " + lecture.getLectureID();
			Anchor hyperlink = new Anchor(display_name);
			hyperlink.getElement().setClassName("lectureLink");
			hyperlink.addClickHandler(new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					controller.showFeedbackScreen(lecture);
				}
			});
			lecture_links.add(hyperlink);
		}
	}
}
